#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{
	pid_t id = fork();
	if(id < 0)
	{
		perror("fork fail");
		exit(1);
	}
	else if(id == 0)
	{
		//child
		printf("i am a child,pid is:%d\n",getpid());
		sleep(5);
		exit(1);
	}
	else
	{	
		//father
		int status = 0;
		pid_t ret = 0;
		do
		{
			ret = waitpid(-1,&status,WNOHANG);//非阻塞等待
			if(ret == 0){
				printf("child is running\n");
			}
			sleep(1);
		}while(ret == 0);
		if(WIFEXITED(status)&&ret == id)
		{
			printf("wait child 5s success,child return code is:%d\n",WEXITSTATUS(status));

		}
		else
		{
			printf("wait child failed,return\n");
			return 1;
		}


	}
	return 0;
}
